<?php

/**
 * This is the model class for table "{{point_log}}".
 *
 * The followings are the available columns in table '{{point_log}}':
 * @property integer $zpl_id
 * @property integer $zm_id
 * @property integer $zpl_type
 * @property integer $zpl_action
 * @property integer $zpl_points
 * @property string $zpl_content
 * @property string $zpl_ip
 * @property string $zo_id
 * @property integer $inputtime
 */
class PointLog extends CActiveRecord
{
	public $action = array(
				1=>'订单',
				2=>'评论',
				3=>'验证',
				4=>'联盟',
				5=>'注册推荐',
			);
	public $type = array(
				1=>'增加',
				2=>'减少',
			);
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return PointLog the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{point_log}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zm_id, zpl_type, zpl_action, zpl_points, inputtime', 'numerical', 'integerOnly'=>true),
			array('zpl_ip', 'length', 'max'=>15),
			array('zo_id', 'length', 'max'=>30),
			array('zpl_content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zpl_id, zm_id, zpl_type, zpl_action, zpl_points, zpl_content, zpl_ip, zo_id, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zpl_id' => 'Zpl',
			'zm_id' => 'Zm',
			'zpl_type' => 'Zpl Type',
			'zpl_action' => 'Zpl Action',
			'zpl_points' => 'Zpl Points',
			'zpl_content' => 'Zpl Content',
			'zpl_ip' => 'Zpl Ip',
			'zo_id' => 'Zo',
			'inputtime' => 'Inputtime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zpl_id',$this->zpl_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zpl_type',$this->zpl_type);
		$criteria->compare('zpl_action',$this->zpl_action);
		$criteria->compare('zpl_points',$this->zpl_points);
		$criteria->compare('zpl_content',$this->zpl_content,true);
		$criteria->compare('zpl_ip',$this->zpl_ip,true);
		$criteria->compare('zo_id',$this->zo_id,true);
		$criteria->compare('inputtime',$this->inputtime);

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/*
	 * 获取会员积分日志
	 * @param string $memberid 会员id
	 * @param string $time 时间：1.近三个月 2.三个月前
	 * @param string $type 类型：1.增加 2.减少
	 * @param string $action 操作类型：1.订单 2.评论 3.验证 4.联盟 5.注册推荐
	 * 
	 * */
	public function getUserPointLogs($memberid,$time=null,$type=null,$action=null){
		$criteria = new CDbCriteria();
		$criteria->compare('zm_id',$memberid);
		$criteria->order = 'inputtime desc';
		if($time==1){
			$criteria->compare('inputtime','>='.(time()-3600*24*30));
		}elseif($time==2){
			$criteria->compare('inputtime','<'.(time()-3600*24*30));
		}
		if($type){
			$criteria->compare('zpl_type',$type);
		}
		if($action){
			$criteria->compare('zpl_action',$action);
		}
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				'pagination'=>array('pageSize'=>10),
		));
	}
	
	protected function beforeSave(){
		if (parent::beforeSave()){
			//判断操作类型,2评论3.验证,4联盟,5注册推荐',
			if($this->zpl_action==2){
				$type = 'compoint';
			}else if($this->zpl_action==3){
				$type = 'checkpoint';
			}
			$val = Setting::model()->findByAttributes(array('zkl_variable'=>$type))->attributes['zkl_valued'];
			$this->zpl_points = $val;
			$this->inputtime  = time();
			return true;
		}else{
			return false;
		}
	}
}